<template>
  <el-dialog :title="$t('reviewRiskList.riskSetting.title')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <!-- 是否开启 -->
      <el-form-item :label="$t('reviewRiskList.manageModule.isopen')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('common.select')">
          <el-option
            v-for="(item,index) in $t('reviewRiskList.riskSetting.state')"
            :key="`${item.value}_${index}`"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="!status && paramsObj" :label="$t('reviewRiskList.riskSetting.table.value')" prop="params">
        <el-col v-for="(item,index) in paramsObj" :key="index" :span="24/(paramsObj.length)">
          <el-input
            v-model="paramsObj[index]"
            clearable
            :maxlength="50"
            style="width: 100%"
            :placeholder="$t('reviewRiskList.riskSetting.table.value')"
          />
        </el-col>
      </el-form-item>
      <el-form-item v-if="status" :label="$t('reviewRiskList.riskSetting.table.value')" prop="params">
        <el-select v-model="formValidate.params" multiple clearable :placeholder="$t('common.select')">
          <el-option
            v-for="item in keysObj"
            :key="item"
            :label="userSocialIdentityCode[item]"
            :value="item"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setRiskReview } from '@/api/risk'
export default {
  name: 'RiskReviewDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    hiddenState: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    paramsObj: {
      type: Array,
      default: function() {
        return []
      }
    },
    keysObj: {
      type: Object,
      default: function() {
        return {}
      }
    },
    userSocialIdentityCode: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateExemptionDays = (rule, value, callback) => {
      if (!this.formValidate.params) {
        callback(new Error(this.$t('reviewRiskList.riskSetting.table.value') + this.$t('common.null')))
      } else {
        callback()
      }
    }
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        descriptor: null,
        paramName: null,
        params: null,
        status: null,
        type: null
      },
      rules: {
        status: [{ required: true, message: this.$t('reviewRiskList.manageModule.isopen') + this.$t('common.null'), trigger: 'blur' }],
        params: [{ required: true, validator: validateExemptionDays, type: 'number', trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    },
    status: {
      get: function() {
        return this.hiddenState
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const arr = JSON.parse(JSON.stringify(this.paramsObj))
          let num = this.paramsObj.length
          if (num < 4) {
            for (num; num < 4; num++) {
              arr[num] = null
            }
          }
          const data = {
            id: this.formValidate.id,
            descriptor: this.formValidate.description,
            paramName: this.formValidate.paramName,
            params: arr,
            status: this.formValidate.status,
            type: this.formValidate.type
          }
          setRiskReview(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .admin-tree{
    margin-top: 20px;
  }
</style>
